package com.sx.jys.ui.vm

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.sx.jys.app.net.HttpRequestCoroutine
import com.sx.jys.app.net.bean.SwitchEnum
import com.sx.jys.app.net.bean.SwithcBean
import com.sx.jys.app.until.CacheUntil
import com.sx.jys.app.until.request2
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData

class SettingVm : BaseViewModel() {
    val showMoreUi = BooleanLiveData()
    var switchStatus = BooleanLiveData()
    var switchZsStatus = BooleanLiveData()
    val ssRatoLiveData = MutableLiveData<SwithcBean>()
    val ssTimeLiveData = MutableLiveData<SwithcBean>()

    fun orderSwitch(open: Boolean) {
        request2({ HttpRequestCoroutine.orderSwitch(if (open) "1" else "2") }, {
            ToastUtils.showShort("策略已" + if (open) "打开" else "关闭")
        }, {
            switchStatus.value = open
        }, isShowDialog = true)
    }

    fun zsSwitch(open: Boolean) {
        request2({ HttpRequestCoroutine.configSetting(SwitchEnum.HEIGHT_SWITCH.mValue,if (open) "1" else "0") }, {
            ToastUtils.showShort("前高前低止损" + if (open) "打开" else "关闭")
        }, {
            switchZsStatus.value = open
        }, isShowDialog = true)
    }

    fun logout(logout: () -> Unit) {
        request2({ HttpRequestCoroutine.logOut() }, {
            logout.invoke()
        }, isShowDialog = true)
    }

    fun reqOrderSwitch() {
        request2({ HttpRequestCoroutine.switchSelect(SwitchEnum.ORDER_SWITCH.mValue) }, {
            switchStatus.value = it.switchStatus()
        })
        request2({ HttpRequestCoroutine.switchSelect(SwitchEnum.HEIGHT_SWITCH.mValue) }, {
            switchZsStatus.value = it.switchStatus()
        })
        request2({ HttpRequestCoroutine.switchSelect(SwitchEnum.SS_TIME.mValue) }, {
            ssTimeLiveData.value = it
        })
        request2({ HttpRequestCoroutine.switchSelect(SwitchEnum.SS_RATO.mValue) }, {
            ssRatoLiveData.value = it
        })
    }

    fun reqSetting(
        keyval: String,
        vals: String
    ) {
        request2({ HttpRequestCoroutine.configSetting(keyval, vals) }, {
            ToastUtils.showShort("设置成功")
            reqOrderSwitch()
        }, isShowDialog = true)
    }
}